package com.qzsoft.zhjd.machine.modules.machinedriver.controller;

import java.util.List;
import java.util.Map;

import com.qzsoft.jeemis.common.validator.ValidatorUtils;
import com.qzsoft.jeemis.common.validator.group.AddGroup;
import com.qzsoft.jeemis.common.validator.group.UpdateGroup;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qzsoft.jeemis.common.utils.Result;
import com.qzsoft.zhjd.common.enums.BizCode;
import com.qzsoft.zhjd.common.utils.ResultUtils;
import com.qzsoft.zhjd.machine.modules.machinedriver.entity.MachineDriverEntity;
import com.qzsoft.zhjd.machine.modules.machinedriver.service.MachineDriverService;

/**
 * @ClassName MachineDriverController
 * @Description 驾驶员控制器
 * @author linhuawei
 * @since 2021-10-29
 */
@RestController
@RequestMapping("/api/machineDriver")
public class MachineDriverController {

    @Autowired
    private MachineDriverService machineDriverService;

    /**
     * @Title page
     * @Description 获取驾驶员分页数据
     * @param params 查询条件
     * @return 驾驶员分页数据
     */
    @RequiresPermissions("api:machineDriver:list")
    @GetMapping("/page")
    public Result<IPage<MachineDriverEntity>> page(@RequestParam Map<String, Object> params) {
        IPage<MachineDriverEntity> page = machineDriverService.page(params);
        return ResultUtils.ok(page);
    }

    /**
     * @Title list
     * @Description 获取驾驶员列表
     * @param params 查询条件
     * @return 驾驶员列表
     */
    @GetMapping("/list")
    public Result<List<MachineDriverEntity>> list(@RequestParam Map<String, Object> params) {
        List<MachineDriverEntity> list = machineDriverService.list(params);
        return ResultUtils.ok(list);
    }

    /**
     * @Title: find
     * @Description: 获取驾驶员
     * @param id 拌和站主键
     * @return 驾驶员
     */
    @RequiresPermissions("api:machineDriver:edit")
    @GetMapping("/find/{id}")
    public Result<MachineDriverEntity> find(@PathVariable("id") String id) {
        MachineDriverEntity entity = machineDriverService.getById(id);
        if (null == entity) {
            return ResultUtils.error(BizCode.DATA_NOT_FOUND);
        }
        return ResultUtils.ok(entity);
    }

    /**
     * @Title: save
     * @Description: 添加驾驶员
     * @param entity 驾驶员
     * @return 添加结果
     */
    @RequiresPermissions("api:machineDriver:save")
    @PostMapping("/save")
    public Result<String> save(@RequestBody MachineDriverEntity entity) {
        ValidatorUtils.validateEntity(entity, AddGroup.class);
        machineDriverService.save(entity);
        return ResultUtils.ok();
    }

    /**
     * @Title: update
     * @Description: 修改驾驶员
     * @param entity 驾驶员
     * @return 修改结果
     */
    @PostMapping("/update")
    public Result<String> update(@RequestBody MachineDriverEntity entity) {
        ValidatorUtils.validateEntity(entity, UpdateGroup.class);
        machineDriverService.updateById(entity);
        return ResultUtils.ok();
    }

    /**
     * @Title: delete
     * @Description: 删除驾驶员
     * @param id 拌和站主键
     * @return 删除结果
     */
    @RequiresPermissions("api:machineDriver:remove")
    @PostMapping("/delete/{id}")
    public Result<String> delete(@PathVariable("id") String id) {
        machineDriverService.removeById(id);
        return ResultUtils.ok();
    }

}
